using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MatcherQueryApi;
using System.Text;

public partial class AutoCompleteProxy : System.Web.UI.Page 
{
    private string matcherServerHost;
    private string matcherServerType = "raw";
    private int matcherServerPort;

    protected void Page_Load(object sender, EventArgs e)
    {
        string q = Request.QueryString["q"];

        if (!String.IsNullOrEmpty(q))
        {
            Response.ContentType = "text/xml"; // xml
            // Response.ContentType = "text/x-json"; // json
            Response.ContentEncoding = Encoding.UTF8;

            Response.Write(GetResult(q));
        }
    }

    private string GetResult(string query)
    {
        try
        {
            IMatcher matcherServer = null;

            if (Page.Cache["matcher"] != null)
            { matcherServer = Page.Cache["matcher"] as IMatcher; }
            else
            {
                // get the matcher server hostname
                matcherServerHost = ConfigurationManager.AppSettings["matcherserver"];

                string type = ConfigurationManager.AppSettings["matcherservertype"];

                // get the matcher server port number
                int port = 15200;
                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["matcherserverport"]))
                    Int32.TryParse(ConfigurationManager.AppSettings["matcherserverport"], out port);
                matcherServerPort = port;

                // validate the configuration values
                if (!String.IsNullOrEmpty(type))
                    matcherServerType = type;

                if (String.IsNullOrEmpty(matcherServerHost))
                    throw new ConfigurationException("No matcher server host specified.");

                // use the xml or json type to change the parsing of the mathcer results.
                // PassThroughResultParser does not parse the response from the matcher server, just forwards the whole response as the match.
                IMatcherResultParser resultParser = null;
                switch (matcherServerType)
                {
                    case "xml": resultParser = new MatcherQueryApi.XmlResultParser(); break;
                    case "json": resultParser = new MatcherQueryApi.JavascriptResultParser(); break;
                    case "raw":
                    default: resultParser = new MatcherQueryApi.PassThroughResultParser(); break;
                }

                matcherServer = new HttpRemoteMatcher(matcherServerHost, matcherServerPort, resultParser);
                Page.Cache["matcher"] = matcherServer;
            }

            // tweaking and re-formatting of the response from the matcher server could be done here.
            // just build the string you would like to send to the caller.
            StringBuilder buffer = new StringBuilder();
            if (matcherServer != null)
            {
                IMatch[] matches = matcherServer.Lookup(query, null);

                foreach (IMatch match in matches)
                {
                    buffer.Append(match.Match);
                }
            }

            return buffer.ToString();
        }
        catch (Exception ex)
        {
            return "";
        }
    }
}
